package com.naxiang.cn.naxiangstram;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.naxiang.cn.utils.FileUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;

public class LocationUpLoad extends AppCompatActivity {

    private static final int REQUEST_TAKE_GALLERY_VIDEO = 0x23;
    private static final int REQUEST_TAKE_GALLERY_VIDEO_KITKAT = 0x24;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_up_load);


    }

    public void choicefile(View view) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            Intent intent = new Intent();
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "选择视频文件"), REQUEST_TAKE_GALLERY_VIDEO);
        } else {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("video/*");
            startActivityForResult(intent, REQUEST_TAKE_GALLERY_VIDEO_KITKAT);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && null != data) {
            Uri originalUri = null;
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                originalUri = data.getData();
            } else if (requestCode == REQUEST_TAKE_GALLERY_VIDEO_KITKAT) {
                originalUri = data.getData();
                final int takeFlags = data.getFlags()
                        & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                // Check for the freshest data.
                getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
            }

            String videoPath = FileUtils.getPath(this, originalUri);
            System.out.println(videoPath);
            upLoadFile(videoPath);
        }else {

            Toast.makeText(LocationUpLoad.this,"读取文件路径失败",Toast.LENGTH_SHORT).show();
        }
    }

    public void upLoadFile(String path){

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);

        if(path.equals(""))
            Toast.makeText(LocationUpLoad.this, "请选择视频后，再点击上传！", Toast.LENGTH_LONG).show();
        else {
            File file = new File(path);
            OkHttpUtils
                    .postFile()
                    .url("http://talk.cocophp.com")
                    .file(file)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int i) {
                            Toast.makeText(LocationUpLoad.this,"上传失败",Toast.LENGTH_SHORT).show();
                            System.out.println("上传失败");
                        }

                        @Override
                        public void onResponse(String s, int i) {
                            Toast.makeText(LocationUpLoad.this,"上传成功",Toast.LENGTH_SHORT).show();

                            System.out.println(s);
                            System.out.println("上传成功");
                        }

                        @Override
                        public void inProgress(float progress, long total, int id) {
                            super.inProgress(progress, total, id);
                        }
                    });

        }

    }
}
